import Toast from '@/../static/vant-weapp/toast/toast'
import config from '../config'
import Fly from 'flyio/dist/npm/wx'

const fly = new Fly()

fly.config.baseURL = config.baseURL
fly.config.timeout = config.timeout

// 请求拦截器
fly.interceptors.request.use((request) => {
  Toast.loading({
    message: '加载中...',
    duration: 0,
    forbidClick: true
  })
  var wxUser = wx.getStorageSync('wxUser')
  var user = wx.getStorageSync('user')
  var token = wx.getStorageSync('token')
  var type = 'ybxxszl'
  if (wxUser !== '' && user !== '' && token !== '') {
    request.headers = {
      'X-Tag': 'flyio',
      'content-type': 'application/json',
      'H-userId': wxUser.wxUserId,
      'H-token': token,
      'H-type': type
    }
  } else {
    request.headers = {
      'X-Tag': 'flyio',
      'content-type': 'application/json',
      'H-type': type
    }
  }
  return request
})

// 响应拦截器
fly.interceptors.response.use(
  (response) => {
    Toast.clear()
    console.log(response.data)
    return response.data
  },
  (error) => {
    Toast.clear()
    console.log(error.response.data)
    return error.response.data
  }
)

export default fly
